<template>
	<!-- 自定义导航栏 -->
	<u-navbar title="获取位置" :is-fixed="true" :border-bottom="false" title-size="28" title-color="#333">
		<view slot="right" class="right">
			<text @click="confirm" style="color: #00c582; font-size: 28rpx">确认</text>
		</view>
	</u-navbar>

	<view class="map-container">
		<map class="map" id="map" ref="map" show-location :latitude="latitude" :longitude="longitude" @regionchange="regionchange"></map>

		<!-- 定位小图标 -->
		<image src="@/static/positioning-icon.png" class="positioning-icon" :style="positioningIconStyle"></image>
	</view>

	<!-- 定位地址选择 -->
	<view class="addreas-box">
		<view class="addreas-box-header">
			<view class="search-box">
				<image src="@/static/search-icon.png" class="search-icon"></image>
				<input @confirm="searchAddress" type="text" placeholder="搜索地点" class="search-input" />
			</view>
		</view>
		<view class="addreas-box-body">
			<view @click="chooseAddress(item)" v-for="item in pois" :key="item.id" id="poisListBox" class="address-item">
				<view class="item-text">{{ item.name }}</view>
				<image v-if="item.id == currentChooseAddress.id" src="@/static/addStore/checked.png" class="choose-icon"></image>
				<image v-else src="@/static/addStore/noCheck.png" class="choose-icon"></image>
			</view>
		</view>
		<image @click="resetPosition" src="@/static/reset-icon.png" class="reset-icon"></image>
	</view>
</template>

<script setup>
import { nextTick, ref } from 'vue';
import { AMapWX } from './libs/amap-wx.130.js';
import { onLoad } from '@dcloudio/uni-app';

// 当前选中的地址
const currentChooseAddress = ref({});

// 经纬度信息
const latitude = ref('31.813584');
const longitude = ref('117.267718');

// 实例化高德地图实例
const map = ref(null);
const mapCtx = new AMapWX({ key: '048cea787e704fa089cd95a3ff7cd2b4' });
const wxMapCtx = wx.createMapContext('map');

// 获取用户经纬度信息
const getUserLocation = () => {
	uni.getLocation({
		type: 'gcj02',
		isHighAccuracy: true,
		success: function (res) {
			console.log('当前位置的经度：' + res.longitude);
			console.log('当前位置的纬度：' + res.latitude);
			latitude.value = res.latitude;
			longitude.value = res.longitude;

			wxMapCtx.moveToLocation({
				latitude: res.latitude,
				longitude: res.longitude,
				success: function (res) {
					console.log('移动到当前位置成功：', res);
				},
				fail: function (res) {
					console.log('移动到当前位置失败：', res);
				}
			});

			reverseGeocode(res.latitude, res.longitude);
		}
	});
};

// 搜索地点
const searchAddress = (e) => {
	console.log(e.detail.value);
	uni.request({
		url: 'https://restapi.amap.com/v3/place/text',
		data: {
			key: '3f96c3f02fa1366472c5ab89fed6e186',
			keywords: e.detail.value
		},
		success: function (res) {
			console.log(res);
			console.log('搜索地点：', res.data.pois);
			pois.value = [];
			nextTick(() => {
				pois.value = res.data.pois;
			});

			if (res.data.pois?.length) {
				console.log('搜索后要移动到的经纬度：', res.data.pois[0].location.split(',')[0], res.data.pois[0].location.split(',')[1]);
				wxMapCtx.moveToLocation({
					latitude: res.data.pois[0].location.split(',')[1],
					longitude: res.data.pois[0].location.split(',')[0],
					success: function (res) {
						console.log('移动到搜索地点成功：', res);
					},
					fail: function (res) {
						console.log('移动到搜索地点失败：', res);
					}
				});
			}
		},
		fail: function (res) {
			console.log('搜索地点失败：', res);
		}
	});
};

// 逆地理位置编码以及获取地图POI信息
const formatted_address = ref('');
const region = ref(''); // 定位的行政区划字符串
const pois = ref([]);
const reverseGeocode = (latitude, longitude) => {
	uni.request({
		url: 'https://restapi.amap.com/v3/geocode/regeo',
		data: {
			key: '3f96c3f02fa1366472c5ab89fed6e186',
			location: longitude + ',' + latitude,
			extensions: 'all',
			radius: 1000
		},
		success: function (res) {
			console.log(res);
			console.log('获取用户的准确定位地址：', res.data.regeocode.formatted_address);
			console.log('获取定位地址周边的POI信息：', res.data.regeocode.pois);

			const { province, city, district } = res.data.regeocode.addressComponent;

			region.value = province + city + district;
			formatted_address.value = res.data.regeocode.formatted_address;
			pois.value = [];
			nextTick(() => {
				pois.value = res.data.regeocode.pois;
				if (pois.value?.length) {
					currentChooseAddress.value = pois.value[0];
				}
			});
		},
		fail: function (res) {
			console.log('逆地理位置编码失败：', res);
		}
	});
};

// 地图移动
const positioningIconStyle = ref({
	transform: 'translate(-50%, -80%)'
});
const regionchange = (e) => {
	console.log(e);
	if (e.type == 'begin') {
		positioningIconStyle.value.transform = 'translate(-50%, -70%) scale(0.9)';
	} else if (e.type == 'end') {
		positioningIconStyle.value.transform = 'translate(-50%, -80%) scale(1)';
		reverseGeocode(e.target.centerLocation.latitude, e.target.centerLocation.longitude);
	}
};

// 恢复定位
const resetPosition = () => {
	getUserLocation();
};

// 选择地址
const chooseAddress = (item) => {
	console.log('选中的地址：', item);
	currentChooseAddress.value = item;
};

// 确认选择地址
const confirm = () => {
	console.log('确认选中的地址：', currentChooseAddress.value);
	uni.$emit('chooseAddress', currentChooseAddress.value);
	uni.navigateBack();
};

onLoad(() => {
	getUserLocation();
});
</script>

<style lang="scss" scoped>
.map-container {
	position: relative;
	height: calc(100vh - 600rpx);
	
	#map {
		width: 100%;
		height: 100%;
	}

	.positioning-icon {
		position: absolute;
		top: 50%;
		left: 50%;
		width: 48rpx;
		height: 92rpx;
		z-index: 999;
		transition: all 0.3s;
	}
}

.addreas-box {
	position: fixed;
	bottom: 0;
	left: 0;
	display: flex;
	flex-direction: column;
	width: 100%;
	height: 600rpx;
	border-radius: 32rpx 32rpx 0 0;
	background-color: #fff;
	z-index: 1000;
	padding-bottom: 30rpx;

	.addreas-box-header {
		min-height: 100rpx;
		padding: 40rpx 48rpx;
		border-radius: 32rpx 32rpx 0 0;
		background: linear-gradient(180deg, rgba(0, 197, 130, 0.12) 0%, rgba(0, 197, 130, 0) 100%);

		.search-box {
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 72rpx;
			padding: 0 24rpx;
			border-radius: 24rpx;
			background-color: #f5f5f5;

			.search-icon {
				width: 36rpx;
				height: 36rpx;
				margin-right: 12rpx;
			}

			.search-input {
				flex: 1;
				text-align: center;
				font-size: 32rpx;
				line-height: 44rpx;
			}
		}
	}

	.addreas-box-body {
		flex: 1;
		height: 200rpx;
		overflow-y: scroll;
		padding: 0rpx 48rpx 68rpx;

		.address-item {
			position: relative;
			display: flex;
			padding: 24rpx 0;
			border-bottom: 2rpx solid #f0f0f0;

			.item-text {
				width: 80%;
				font-size: 28rpx;
				font-weight: 500;
				line-height: 40rpx;
			}

			.choose-icon {
				position: absolute;
				top: 28rpx;
				right: 0;
				width: 32rpx;
				height: 32rpx;
			}
		}
	}

	.reset-icon {
		position: absolute;
		width: 88rpx;
		height: 88rpx;
		top: 0;
		left: 48rpx;
		transform: translateY(-100%);
	}
}
</style>
